package top.vains.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.vains.entity.Role;
import top.vains.entity.User;
import top.vains.entity.customer.Pagination;
import top.vains.mapper.UserMapper;
import top.vains.service.IRoleService;
import top.vains.service.IUserService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author vains
 * @since 2020-02-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final IRoleService roleService;

    @Autowired
    public UserServiceImpl(IRoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public User login(String number, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("number", number);
        wrapper.eq("password", password);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void getUsers(Pagination pagination) {
        Page<Object> page = PageMethod.startPage(pagination.getCurrent(), pagination.getSize());
        List<User> users = baseMapper.selectList(null);
        pagination.setTotal(page.getTotal());
        pagination.setData(users);
    }

    @Override
    public List<Role> getUserHaveRole(Integer userId) {
        return baseMapper.getUserHaveRole(userId);
    }

    @Override
    public List<Role> getUserNotHaveRole(Integer userId) {
        List<Role> userHaveRole = this.getUserHaveRole(userId);
        List<Integer> roleId = userHaveRole.stream().map(Role::getId).collect(Collectors.toList());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.notIn("id", roleId);
        return roleService.list(wrapper);
    }
}
